<?php
	
    /**
     * Apologizes to user with message.
     */
	 
	
    function apologize($message)
    {
        render("apology.php", array("message" => $message));
        exit;
    }
    
    function check_in_range($start_date, $end_date, $date_from_user)
    {
      // Convert to timestamp
      $start_ts = strtotime($start_date);
      $end_ts = strtotime($end_date);
      $user_ts = strtotime($date_from_user);
      // Check that user date is between start & end
      return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
    }
	//get period ID from date
	function getpid($currentdate)
	{
	$found=0;
	  $dates = query('SELECT * FROM periods');
  foreach($dates as $date)
  {
    if(check_in_range($date['period_from'], $date['period_to'], $currentdate))
    {
      return $date["period_id"];
	  $found=1;
      break;
    }

  }
if ($found==0)
{
	return -1;
}
  }
	function getNameFromNumber($num) {
    $numeric = $num % 26;
    $letter = chr(65 + $numeric);
    $num2 = intval($num / 26);
    if ($num2 > 0) {
        return getNameFromNumber($num2 - 1) . $letter;
    } else {
        return $letter;
    }
}
    
    
    function string_trim($string, $trimLength = 40) 
    {
      $length = strlen($string);
      mb_internal_encoding("UTF-8");
      if ($length > $trimLength) {
          $count = 0;
          $prevCount = 0;
          $array = explode(" ", $string);
          foreach ($array as $word) {
              $count = $count + strlen($word);
              $count = $count + 1;
              if ($count > ($trimLength - 3)) {
                  return mb_substr($string, 0, $prevCount) . "...";
              }
              $prevCount = $count;
          }
      } else {
          return $string;
      }
  }
    /**
     * Facilitates debugging by dumping contents of variable
     * to browser.
     */
    function dump($variable)
    {
        require($_SERVER['DOCUMENT_ROOT']."/nazel/templates/dump.php");
        exit;
    }


    
    /**
     * Returns a stock by symbol (case-insensitively) else false if not found.
    
    function lookup($symbol)
    {
        // reject symbols that start with ^
        if (preg_match("/^\^/", $symbol))
        {
            return false;
        }

        // reject symbols that contain commas
        if (preg_match("/,/", $symbol))
        {
            return false;
        }

        // open connection to Yahoo
        $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s=$symbol", "r");
        if ($handle === false)
        {
            return false;
        }

        // download first line of CSV file
        $data = fgetcsv($handle);
        if ($data === false || count($data) == 1)
        {
            return false;
        }

        // close connection to Yahoo
        fclose($handle);

        // ensure symbol was found
        if ($data[2] === "0.00")
        {
            return false;
        }

        // return stock as an associate array
        return [
            "symbol" => $data[0],
            "name" => $data[1],
            "price" => $data[2],
        ];
    }
	*/
    /**
     * Executes SQL statement, possibly with parameters, returning
     * an array of all rows in result set or false on (non-fatal) error.
     */
    function query(/* $sql [, ... ] */)
    {
      
        // SQL statement
        $sql = func_get_arg(0);
      
        // parameters, if any
        $parameters = array_slice(func_get_args(), 1);
        if(isset($parameters[0]) && is_array($parameters[0]))
        {
          $parameter2 = array_slice($parameters[0], 0);
         
        }
        // try to connect to database
        static $handle;
        if (!isset($handle))
        {
		
            try
            {
                // connect to database
                $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD,array(PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));

                // ensure that PDO::prepare returns false when passed invalid SQL
                $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
			
            }
            catch (Exception $e)
            {
                // trigger (big, orange) error
                trigger_error($e->getMessage(), E_USER_ERROR);
                exit;
            }
        }
        // prepare SQL statement
        if($sql == "getlastid") 
          return  $handle->lastInsertId();
        else
          $statement = $handle->prepare($sql);
        
        if ($statement === false)
        {
            // trigger (big, orange) error
            
            exit;
        }
			
		if(isset($parameters[1]) && $parameters[1] === "like")
			$results = $statement->execute(array("%$parameters[0]%"));
			
		else {
      if(isset($parameter2)) //passing array of arrays
      {
        $i=0;
        foreach($parameter2 as $para){
           if(is_array($para)) //in account for multi categories
           {
            foreach($para as $element)
            {
             $statement->bindValue(++$i, $element);
            }
           }
           else
           {
             $statement->bindValue(++$i, $para);
           }
        }
        $results = $statement->execute();
      }
      else
      {
			// execute SQL statement-- genreal case
        $results = $statement->execute($parameters);
      }
		}
      // return result set's rows, if any and if the query is select :)
       
        if ($results !== false)
        {
			if (preg_match('/(select)/i',$sql))
			{
            	return $statement->fetchAll(PDO::FETCH_ASSOC);
			}
			else
			{
				return true;		
			}
        }
        else
        {
		
            return false;
        }
    }

    /**
     * Redirects user to destination, which can be
     * a URL or a relative path on the local host.
     */
    function redirect($destination)
    {
        // handle URL
        if (preg_match("/^https?:\/\//", $destination))
        {
            header("Location: " . $destination);
        }

        // handle absolute path
        else if (preg_match("/^\//", $destination))
        {
            $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
            $host = $_SERVER["HTTP_HOST"];
            header("Location: $protocol://$host$destination");
        }

        // handle relative path
        else
        {
            // adapted from http://www.php.net/header
            $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
            $host = $_SERVER["HTTP_HOST"];
            $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
            header("Location: $protocol://$host$path/$destination");
        }

        // exit immediately since we're redirecting anyway
        exit;
    }
    
    

    /**
     * Renders template, passing in values.
     */
    function render($template,$values)
    {
        // if template exists, render it
       if (file_exists($_SERVER['DOCUMENT_ROOT']."/nazel/templates/$template"))
        {
            // extract variables into local scope
            extract($values);

            // render header
            require($_SERVER['DOCUMENT_ROOT']."/nazel/templates/header.php");

            // render template
            require($_SERVER['DOCUMENT_ROOT']."/nazel/templates/$template");

            // render footer
            require($_SERVER['DOCUMENT_ROOT']."/nazel/templates/footer.php");
        }

        // else err
        else
        {
            trigger_error("Invalid template: $template", E_USER_ERROR);
        }
    }
	
	//set the period price of an apartment
	function setpp($aptid,$pid,$cat,$price)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	
	//get the previous rates 
	$info=query("SELECT `$cat` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	
	//get the result ready for editing
	$priceinfo=array();
	if ($info["$cat"]!="")
	{
	$priceinfo=explode("_",$info["$cat"]);
	}
	
	//separator
	$sep="";
	$found=0;
	$result="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	
	//modify the period that needs to be modified
	if ($ninfo[0]==$pid)
	{
	$found=1;
	$ninfo[1]=$price;
	}
	$result.=$sep.$ninfo[0].'|'.$ninfo[1];
	$sep="_";
	}
	
	//Check if the period already exists and was modified or it needs to be added
	if ($found!=1)
	{
	$result.="$sep$pid|$price";
	}
	
	}
	//No data were available
	else
	{
	return -1;
	}
	
	//Get binds ready to update the price
	$nbind['cat']=$result;
	$nbind['id']=$aptid;
	
	//It's not possible to check if the query was ok or not (a bug maybe ?)
	query("UPDATE `apartments` SET `$cat`=? WHERE `id` = ?",$nbind);
	
	//bind for getting the apt. of a specific period
	$bindz['pid']=$pid;
	//get the list of apt. related to that period
	$ainfo=query("SELECT `apt` FROM `periods` WHERE `period_id` = ? LIMIT 1",$bindz);
	
	//Make sure that there's such a period
	if (!empty($ainfo))
	{
	$ainfo=$ainfo[0];
	//Check if the period has previous apt. 
	if ($ainfo['apt']!="")
	{
	$nainfo=explode("_",$ainfo['apt']);
	//loop through them
	$resultx="";
	$found=0;
	$sep="";
	foreach ($nainfo as $apt)
	{
	if ($apt==$aptid)
	{
	$found=1;
	}
	$resultx.="$sep$apt";
	$sep="_";
	}
	
	if ($found!=1)
	{
	$resultx.="$sep$aptid";
	}
	
	}
	else
	{
	$resultx="$aptid";
	}
	}
	else
	{
	return -1;
	}
	$lbind['res']=$resultx;
	$lbind['id']=$pid;
	query("UPDATE `periods` SET `apt`=? WHERE `period_id` = ?",$lbind);
	}
	
	//get the period price of an apartment
	function getpp($aptid,$pid,$cat)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	
	//get the previous rates 
	$info=query("SELECT `$cat` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	//get the result ready for editing
	$priceinfo=explode("_",$info[$cat]);
	$result=0;
	foreach ($priceinfo as $rate)
	{
	$ninfo=explode("|",$rate);
	if ($ninfo[0]==$pid)
	{
	$result=$ninfo[1];
	break 1;
	}
	}
	return $result;
	}
	else
	{
	return 0;
	}
	}
	
	//set the max discount for period
	function setmd($aptid,$pid,$discount)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	//get the previous rates 
	$info=query("SELECT `heighest_discount` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	
	$priceinfo=array();
	//get the result ready for editing
	if ($info["heighest_discount"]!="")
	{
	$priceinfo=explode("_",$info["heighest_discount"]);
	}
	//separator
	$sep="";
	$found=0;
	$result="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	
	//modify the period that needs to be modified
	if ($ninfo[0]==$pid)
	{
	$found=1;
	$ninfo[1]=$discount;
	}
	$result.=$sep.$ninfo[0].'|'.$ninfo[1];
	$sep="_";
	}
	
	//Check if the period already exists and was modified or it needs to be added
	if ($found!=1)
	{
	$result.="$sep$pid|$discount";
	}
	
	}
	//No data were available
	else
	{
	return -1;
	}
	
	//Get binds ready to update the price
	$nbind['cat']=$result;
	$nbind['id']=$aptid;
	
	//It's not possible to check if the query was ok or not (a bug maybe ?)
	query("UPDATE `apartments` SET `heighest_discount`=? WHERE `id` = ?",$nbind);
	
	//bind for getting the apt. of a specific period
	$bindz['pid']=$pid;
	//get the list of apt. related to that period
	$ainfo=query("SELECT `apt` FROM `periods` WHERE `period_id` = ? LIMIT 1",$bindz);

	
	//Make sure that there's such a period
	if (!empty($ainfo))
	{
	$ainfo=$ainfo[0];
	//Check if the period has previous apt. 
	if ($ainfo['apt']!="")
	{
	$nainfo=explode("_",$ainfo['apt']);
	//loop through them
	$resultx="";
	$found=0;
	$sep="";
	foreach ($nainfo as $apt)
	{
	if ($apt==$aptid)
	{
	$found=1;
	}
	$resultx.="$sep$apt";
	$sep="_";
	}
	
	if ($found!=1)
	{
	$resultx.="$sep$aptid";
	}
	
	}
	else
	{
	$resultx="$aptid";
	}
	}
	else
	{
	return -1;
	}
	$lbind['res']=$resultx;
	$lbind['id']=$pid;
	query("UPDATE `periods` SET `apt`=? WHERE `period_id` = ?",$lbind);
	}
	
	//get the max discount for period
	function getmd($aptid,$pid)
	{
		//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	
	//get the previous rates 
	$info=query("SELECT `heighest_discount` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	//get the result ready for editing
	$priceinfo=explode("_",$info["heighest_discount"]);
	$result=0;
	foreach ($priceinfo as $rate)
	{
	$ninfo=explode("|",$rate);
	if ($ninfo[0]==$pid)
	{
	$result=$ninfo[1];
	break 1;
	}
	}
	return $result;
	}
	else
	{
	return 0;
	}
	}
	
	//delete max discount or price for a specific period
	function delmp ($aptid,$pid,$filter)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	if ($filter==1)
	{
	$cat="`heighest_discount`";
	}
	elseif ($filter==0)
	{
	$cat="`weekly`,`monthly`,`daily`";
	}
	
	//get the previous rates 
	$info=query("SELECT $cat FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	//Delete Period Price
	if ($filter==0)
	{
	$priceinfo=array();
	//Check if daily is empty
		$dresult="";
	if ($info["daily"]!="")
	{
	$priceinfo=explode("_",$info["daily"]);
	//separator
	$sep="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$dresult.=$sep.$ninfo[0].'|'.$ninfo[1];
	}
	if ($dresult!="")
	{
	$sep="_";
	}
	}
	}
	
	//Check if weekly is empty
	$wresult="";
	if($info["weekly"]!="")
	{
	$priceinfo=explode("_",$info["weekly"]);
	//separator
	$sep="";	
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
		//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$wresult.=$sep.$ninfo[0].'|'.$ninfo[1];
	}
	if ($wresult!="")
	{
	$sep="_";
	}
	}
	}
	//Check if monthly is empty
	$mresult="";
	if($info["monthly"]!="")
	{
	$priceinfo=explode("_",$info["monthly"]);
	//separator
	$sep="";
	$found=0;
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$mresult.=$sep.$ninfo[0].'|'.$ninfo[1];
	}
	
	if ($mresult!="")
	{
	$sep="_";
	}
	
	}
	}
	//Get binds ready to update the price
	$nbind['d']=$dresult;
	$nbind['w']=$wresult;
	$nbind['m']=$mresult;
	$nbind['id']=$aptid;
	//update the max discount array
	query("UPDATE `apartments` SET `daily`=?,`weekly`=?,`monthly`=? WHERE `id` = ?",$nbind);
	}
	//delete period max discount
	elseif ($filter==1)
	{
	$priceinfo=array();
	//get the result ready for editing
	if ($info["heighest_discount"]!="")
	{
	$priceinfo=explode("_",$info["heighest_discount"]);
	//separator
	$sep="";
	$result="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$result.=$sep.$ninfo[0].'|'.$ninfo[1];
	}
	if ($result!="")
	{
	$sep="_";
	}
	}
		//Get binds ready to update the price
		$nbind['cat']=$result;
		$nbind['id']=$aptid;
		//update the max discount array
		query("UPDATE `apartments` SET `heighest_discount`=? WHERE `id` = ?",$nbind);
	}
	}
	
	}
	else
	{
	return -1;
	}
	}
	
	//Delete period correctly
	function delperiod ($pid,$oid)
	{
	//bind for getting the apt. of a specific period
	$bindz['pid']=$pid;
	$bindz['oid']=$oid;
	//get the list of apt. related to that period
	$ainfo=query("SELECT `apt` FROM `periods` WHERE `period_id` = ? AND `owner_id`=? LIMIT 1",$bindz);	
	//Make sure that there's such a period
	if (!empty($ainfo))
	{
	$ainfo=$ainfo[0];
	//Check if the period has previous apt. 
	if ($ainfo['apt']!="")
	{
	$nainfo=explode("_",$ainfo['apt']);
	foreach ($nainfo as $aptid)
	{
	delmp($aptid,$pid,0);
	delmp($aptid,$pid,1);
	}
	}
	}
	else
	{
	return -1;
	}
	query("DELETE FROM `periods` WHERE `period_id` = ? AND `owner_id`=?",$bindz);
	}
	
    /**
     * Renders template, passing in values.
     */
    function renderWithoutHeader($template, $values)
    {
        // if template exists, render it
        if (file_exists($_SERVER['DOCUMENT_ROOT']."/nazel/templates/$template"))
        {
            // extract variables into local scope
            extract($values);


            // render template
            require($_SERVER['DOCUMENT_ROOT']."/nazel/templates/$template");
        }

        // else err
        else
        {
            trigger_error("Invalid template: $template", E_USER_ERROR);
        }
    }
    
  function balanceSMS($viewResult=1)
  {
    global $arrayBalance;
    $url = "http://www.mobily.ws/api/balance.php";
    $stringToPost = "mobile=".PHONE."&password=".M_PASSWORD;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringToPost);
    $result = curl_exec($ch);

    $results = explode("/", $result);
    $results['current'] = $results[1];
    $results['total'] = $results[0];
    return $results;
    
  }	
  
  function sendStatus($viewResult=1)
  {
    global $arraySendStatus;	
    $url = "www.mobily.ws/api/sendStatus.php";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    $result = curl_exec($ch);

    return $result;
  }
  
  function sendSMS($numbers, $sender, $msg, $timeSend=0, $dateSend=0, $deleteKey=0, $viewResult=1)
  {
  	$defaultCode="00966";
  	//Make sure the country code is ok before sending
  	$number=explode(',',$numbers);
	//Clear Numbers String To Fill Again with correct numbers
	$numbers="";
	$separator="";//separator to use before numbers a.k.a comma should be empty if no numbers yet
  	foreach ($number as $oneNumber)
	{
		if (preg_match("/^(00|\+)/",$oneNumber)==FALSE)
		{
			$oneNumber=$defaultCode.$oneNumber;
		}
		$numbers=$numbers.$separator.$oneNumber;
		$separator=",";
	}
	/*
    global $arraySendMsg;
    $url = "www.mobily.ws/api/msgSend.php";
    $applicationType = "24"; 
    $sender = urlencode($sender);
    $domainName = $_SERVER['SERVER_NAME'];
    $stringToPost = "mobile=".PHONE."&password=".M_PASSWORD."&numbers=".$numbers."&sender=".$sender."&msg=".$msg."&timeSend=".$timeSend."&dateSend=".$dateSend."&applicationType=".$applicationType."&domainName=".$domainName."&deleteKey=".$deleteKey;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $stringToPost);
    $result = curl_exec($ch);
    return $result;
	*/
  }
  

  function convertToUnicode($message)
  {
    $chrArray[0] = "¡";
    $unicodeArray[0] = "060C";
    $chrArray[1] = "º";
    $unicodeArray[1] = "061B";
    $chrArray[2] = "¿";
    $unicodeArray[2] = "061F";
    $chrArray[3] = "Á";
    $unicodeArray[3] = "0621";
    $chrArray[4] = "Â";
    $unicodeArray[4] = "0622";
    $chrArray[5] = "Ã";
    $unicodeArray[5] = "0623";
    $chrArray[6] = "Ä";
    $unicodeArray[6] = "0624";
    $chrArray[7] = "Å";
    $unicodeArray[7] = "0625";
    $chrArray[8] = "Æ";
    $unicodeArray[8] = "0626";
    $chrArray[9] = "Ç";
    $unicodeArray[9] = "0627";
    $chrArray[10] = "È";
    $unicodeArray[10] = "0628";
    $chrArray[11] = "É";
    $unicodeArray[11] = "0629";
    $chrArray[12] = "Ê";
    $unicodeArray[12] = "062A";
    $chrArray[13] = "Ë";
    $unicodeArray[13] = "062B";
    $chrArray[14] = "Ì";
    $unicodeArray[14] = "062C";
    $chrArray[15] = "Í";
    $unicodeArray[15] = "062D";
    $chrArray[16] = "Î";
    $unicodeArray[16] = "062E";
    $chrArray[17] = "Ï";
    $unicodeArray[17] = "062F";
    $chrArray[18] = "Ð";
    $unicodeArray[18] = "0630";
    $chrArray[19] = "Ñ";
    $unicodeArray[19] = "0631";
    $chrArray[20] = "Ò";
    $unicodeArray[20] = "0632";
    $chrArray[21] = "Ó";
    $unicodeArray[21] = "0633";
    $chrArray[22] = "Ô";
    $unicodeArray[22] = "0634";
    $chrArray[23] = "Õ";
    $unicodeArray[23] = "0635";
    $chrArray[24] = "Ö";
    $unicodeArray[24] = "0636";
    $chrArray[25] = "Ø";
    $unicodeArray[25] = "0637";
    $chrArray[26] = "Ù";
    $unicodeArray[26] = "0638";
    $chrArray[27] = "Ú";
    $unicodeArray[27] = "0639";
    $chrArray[28] = "Û";
    $unicodeArray[28] = "063A";
    $chrArray[29] = "Ý";
    $unicodeArray[29] = "0641";
    $chrArray[30] = "Þ";
    $unicodeArray[30] = "0642";
    $chrArray[31] = "ß";
    $unicodeArray[31] = "0643";
    $chrArray[32] = "á";
    $unicodeArray[32] = "0644";
    $chrArray[33] = "ã";
    $unicodeArray[33] = "0645";
    $chrArray[34] = "ä";
    $unicodeArray[34] = "0646";
    $chrArray[35] = "å";
    $unicodeArray[35] = "0647";
    $chrArray[36] = "æ";
    $unicodeArray[36] = "0648";
    $chrArray[37] = "ì";
    $unicodeArray[37] = "0649";
    $chrArray[38] = "í";
    $unicodeArray[38] = "064A";
    $chrArray[39] = "Ü";
    $unicodeArray[39] = "0640";
    $chrArray[40] = "ð";
    $unicodeArray[40] = "064B";
    $chrArray[41] = "ñ";
    $unicodeArray[41] = "064C";
    $chrArray[42] = "ò";
    $unicodeArray[42] = "064D";
    $chrArray[43] = "ó";
    $unicodeArray[43] = "064E";
    $chrArray[44] = "õ";
    $unicodeArray[44] = "064F";
    $chrArray[45] = "ö";
    $unicodeArray[45] = "0650";
    $chrArray[46] = "ø";
    $unicodeArray[46] = "0651";
    $chrArray[47] = "ú";
    $unicodeArray[47] = "0652";
    $chrArray[48] = "!";
    $unicodeArray[48] = "0021";
    $chrArray[49]='"';
    $unicodeArray[49] = "0022";
    $chrArray[50] = "#";
    $unicodeArray[50] = "0023";
    $chrArray[51] = "$";
    $unicodeArray[51] = "0024";
    $chrArray[52] = "%";
    $unicodeArray[52] = "0025";
    $chrArray[53] = "&";
    $unicodeArray[53] = "0026";
    $chrArray[54] = "'";
    $unicodeArray[54] = "0027";
    $chrArray[55] = "(";
    $unicodeArray[55] = "0028";
    $chrArray[56] = ")";
    $unicodeArray[56] = "0029";
    $chrArray[57] = "*";
    $unicodeArray[57] = "002A";
    $chrArray[58] = "+";
    $unicodeArray[58] = "002B";
    $chrArray[59] = ",";
    $unicodeArray[59] = "002C";
    $chrArray[60] = "-";
    $unicodeArray[60] = "002D";
    $chrArray[61] = ".";
    $unicodeArray[61] = "002E";
    $chrArray[62] = "/";
    $unicodeArray[62] = "002F";
    $chrArray[63] = "0";
    $unicodeArray[63] = "0030";
    $chrArray[64] = "1";
    $unicodeArray[64] = "0031";
    $chrArray[65] = "2";
    $unicodeArray[65] = "0032";
    $chrArray[66] = "3";
    $unicodeArray[66] = "0033";
    $chrArray[67] = "4";
    $unicodeArray[67] = "0034";
    $chrArray[68] = "5";
    $unicodeArray[68] = "0035";
    $chrArray[69] = "6";
    $unicodeArray[69] = "0036";
    $chrArray[70] = "7";
    $unicodeArray[70] = "0037";
    $chrArray[71] = "8";
    $unicodeArray[71] = "0038";
    $chrArray[72] = "9";
    $unicodeArray[72] = "0039";
    $chrArray[73] = ":";
    $unicodeArray[73] = "003A";
    $chrArray[74] = ";";
    $unicodeArray[74] = "003B";
    $chrArray[75] = "<";
    $unicodeArray[75] = "003C";
    $chrArray[76] = "=";
    $unicodeArray[76] = "003D";
    $chrArray[77] = ">";
    $unicodeArray[77] = "003E";
    $chrArray[78] = "?";
    $unicodeArray[78] = "003F";
    $chrArray[79] = "@";
    $unicodeArray[79] = "0040";
    $chrArray[80] = "A";
    $unicodeArray[80] = "0041";
    $chrArray[81] = "B";
    $unicodeArray[81] = "0042";
    $chrArray[82] = "C";
    $unicodeArray[82] = "0043";
    $chrArray[83] = "D";
    $unicodeArray[83] = "0044";
    $chrArray[84] = "E";
    $unicodeArray[84] = "0045";
    $chrArray[85] = "F";
    $unicodeArray[85] = "0046";
    $chrArray[86] = "G";
    $unicodeArray[86] = "0047";
    $chrArray[87] = "H";
    $unicodeArray[87] = "0048";
    $chrArray[88] = "I";
    $unicodeArray[88] = "0049";
    $chrArray[89] = "J";
    $unicodeArray[89] = "004A";
    $chrArray[90] = "K";
    $unicodeArray[90] = "004B";
    $chrArray[91] = "L";
    $unicodeArray[91] = "004C";
    $chrArray[92] = "M";
    $unicodeArray[92] = "004D";
    $chrArray[93] = "N";
    $unicodeArray[93] = "004E";
    $chrArray[94] = "O";
    $unicodeArray[94] = "004F";
    $chrArray[95] = "P";
    $unicodeArray[95] = "0050";
    $chrArray[96] = "Q";
    $unicodeArray[96] = "0051";
    $chrArray[97] = "R";
    $unicodeArray[97] = "0052";
    $chrArray[98] = "S";
    $unicodeArray[98] = "0053";
    $chrArray[99] = "T";
    $unicodeArray[99] = "0054";
    $chrArray[100] = "U";
    $unicodeArray[100] = "0055";
    $chrArray[101] = "V";
    $unicodeArray[101] = "0056";
    $chrArray[102] = "W";
    $unicodeArray[102] = "0057";
    $chrArray[103] = "X";
    $unicodeArray[103] = "0058";
    $chrArray[104] = "Y";
    $unicodeArray[104] = "0059";
    $chrArray[105] = "Z";
    $unicodeArray[105] = "005A";
    $chrArray[106] = "[";
    $unicodeArray[106] = "005B";
    $char="\ ";
    $chrArray[107]=trim($char);
    $unicodeArray[107] = "005C";
    $chrArray[108] = "]";
    $unicodeArray[108] = "005D";
    $chrArray[109] = "^";
    $unicodeArray[109] = "005E";
    $chrArray[110] = "_";
    $unicodeArray[110] = "005F";
    $chrArray[111] = "`";
    $unicodeArray[111] = "0060";
    $chrArray[112] = "a";
    $unicodeArray[112] = "0061";
    $chrArray[113] = "b";
    $unicodeArray[113] = "0062";
    $chrArray[114] = "c";
    $unicodeArray[114] = "0063";
    $chrArray[115] = "d";
    $unicodeArray[115] = "0064";
    $chrArray[116] = "e";
    $unicodeArray[116] = "0065";
    $chrArray[117] = "f";
    $unicodeArray[117] = "0066";
    $chrArray[118] = "g";
    $unicodeArray[118] = "0067";
    $chrArray[119] = "h";
    $unicodeArray[119] = "0068";
    $chrArray[120] = "i";
    $unicodeArray[120] = "0069";
    $chrArray[121] = "j";
    $unicodeArray[121] = "006A";
    $chrArray[122] = "k";
    $unicodeArray[122] = "006B";
    $chrArray[123] = "l";
    $unicodeArray[123] = "006C";
    $chrArray[124] = "m";
    $unicodeArray[124] = "006D";
    $chrArray[125] = "n";
    $unicodeArray[125] = "006E";
    $chrArray[126] = "o";
    $unicodeArray[126] = "006F";
    $chrArray[127] = "p";
    $unicodeArray[127] = "0070";
    $chrArray[128] = "q";
    $unicodeArray[128] = "0071";
    $chrArray[129] = "r";
    $unicodeArray[129] = "0072";
    $chrArray[130] = "s";
    $unicodeArray[130] = "0073";
    $chrArray[131] = "t";
    $unicodeArray[131] = "0074";
    $chrArray[132] = "u";
    $unicodeArray[132] = "0075";
    $chrArray[133] = "v";
    $unicodeArray[133] = "0076";
    $chrArray[134] = "w";
    $unicodeArray[134] = "0077";
    $chrArray[135] = "x";
    $unicodeArray[135] = "0078";
    $chrArray[136] = "y";
    $unicodeArray[136] = "0079";
    $chrArray[137] = "z";
    $unicodeArray[137] = "007A";
    $chrArray[138] = "{";
    $unicodeArray[138] = "007B";
    $chrArray[139] = "|";
    $unicodeArray[139] = "007C";
    $chrArray[140] = "}";
    $unicodeArray[140] = "007D";
    $chrArray[141] = "~";
    $unicodeArray[141] = "007E";
    $chrArray[142] = "©";
    $unicodeArray[142] = "00A9";
    $chrArray[143] = "®";
    $unicodeArray[143] = "00AE";
    $chrArray[144] = "÷";
    $unicodeArray[144] = "00F7";
    $chrArray[145] = "×";
    $unicodeArray[145] = "00F7";
    $chrArray[146] = "§";
    $unicodeArray[146] = "00A7";
    $chrArray[147] = " ";
    $unicodeArray[147] = "0020";
    $chrArray[148] = "\n";
    $unicodeArray[148] = "000D";
    $chrArray[149] = "\r";
    $unicodeArray[149] = "000A";

    $strResult = "";
    for($i=0; $i<strlen($message); $i++)
    {
      if(in_array(substr($message,$i,1), $chrArray))
      $strResult.= $unicodeArray[array_search(substr($message,$i,1), $chrArray)];
    }
    return $strResult;
  }
  
  	//set the period price of an apartment for co
	function cosetpp($aptid,$pid,$cat,$price,$coid)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	
	//get the previous rates 
	$info=query("SELECT `$cat` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	
	//get the result ready for editing
	$priceinfo=array();
	if ($info["$cat"]!="")
	{
	$priceinfo=explode("_",$info["$cat"]);
	}
	
	//separator
	$sep="";
	$found=0;
	$result="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode('|',$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	
	//modify the period that needs to be modified
	if ($ninfo[0]==$pid&&$ninfo[2]==$coid)
	{
	$found=1;
	$ninfo[1]=$price;
	}
	$result.=$sep.$ninfo[0].'|'.$ninfo[1].'|'.$ninfo[2];
	$sep="_";
	}
	
	//Check if the period already exists and was modified or it needs to be added
	if ($found!=1)
	{
	$result.="$sep$pid|$price|$coid";
	}
	
	}
	//No data were available
	else
	{
	return -1;
	}
	
	//Get binds ready to update the price
	$nbind['cat']=$result;
	$nbind['id']=$aptid;
	
	//It's not possible to check if the query was ok or not (a bug maybe ?)
	query("UPDATE `apartments` SET `$cat`=? WHERE `id` = ?",$nbind);
	
	//bind for getting the apt. of a specific period
	$bindz['pid']=$pid;
	$bindz['coid']=$coid;
	//get the list of apt. related to that period
	$ainfo=query("SELECT `apt` FROM `co_periods` WHERE `period_id` = ? AND `co_id`=? LIMIT 1",$bindz);
	
	//Make sure that there's such a period
	if (!empty($ainfo))
	{
	$ainfo=$ainfo[0];
	//Check if the period has previous apt. 
	if ($ainfo['apt']!="")
	{
	$nainfo=explode("_",$ainfo['apt']);
	//loop through them
	$resultx="";
	$found=0;
	$sep="";
	foreach ($nainfo as $apt)
	{
	if ($apt==$aptid)
	{
	$found=1;
	}
	$resultx.="$sep$apt";
	$sep="_";
	}
	
	if ($found!=1)
	{
	$resultx.="$sep$aptid";
	}
	
	}
	else
	{
	$resultx="$aptid";
	}
	}
	else
	{
	return -1;
	}
	$lbind['res']=$resultx;
	$lbind['id']=$pid;
	$lbind['coid']=$coid;
	query("UPDATE `co_periods` SET `apt`=? WHERE `period_id` = ? AND `co_id`=?",$lbind);
	}
	
	//get the period price of an apartment related to co
	function cogetpp($aptid,$pid,$cat,$coid)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	
	//get the previous rates 
	$info=query("SELECT `$cat` FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	//get the result ready for editing
	$priceinfo=explode("_",$info[$cat]);
	$result=0;
	foreach ($priceinfo as $rate)
	{
	$ninfo=explode("|",$rate);
	if ($ninfo[0]==$pid&&$ninfo[2]==$coid)
	{
	$result=$ninfo[1];
	break 1;
	}
	}
	return $result;
	}
	else
	{
	return 0;
	}
	}
	
		//delete price for a specific period related to company
	function codelp ($aptid,$pid)
	{
	//array to hold the binds
	$bind=array();
	$bind['id']=$aptid;
	$cat="`co_weekly`,`co_monthly`,`co_daily`";
	
	//get the previous rates 
	$info=query("SELECT $cat FROM `apartments` WHERE `id` = ? LIMIT 1",$bind);
	if (!empty($info))
	{
	//make it easier to access
	$info=$info[0];
	//Delete Period Price
	$priceinfo=array();
	//Check if daily is empty
	$dresult="";
	if ($info["co_daily"]!="")
	{
	$priceinfo=explode("_",$info["co_daily"]);
	//separator
	$sep="";
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$dresult.=$sep.$ninfo[0].'|'.$ninfo[1].'|'.$ninfo[2];
	}
	if ($dresult!="")
	{
	$sep="_";
	}
	}
	}
	
	//Check if weekly is empty
	$wresult="";
	if($info["co_weekly"]!="")
	{
	$priceinfo=explode("_",$info["co_weekly"]);
	//separator
	$sep="";	
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$wresult.=$sep.$ninfo[0].'|'.$ninfo[1].'|'.$ninfo[2];
	}
	if ($wresult!="")
	{
	$sep="_";
	}
	}
	}
	//Check if monthly is empty
	$mresult="";
	if($info["co_monthly"]!="")
	{
	$priceinfo=explode("_",$info["co_monthly"]);
	//separator
	$sep="";
	$found=0;
	//loop through array
	foreach ($priceinfo as $rate)
	{
	//get the period and it's price
	$ninfo=explode("|",$rate);
	//avoiding the old 0 default value
	if (!isset($ninfo[1]))
	{
	$ninfo[1]=0;
	}
	//modify the period that needs to be modified
	if ($ninfo[0]!=$pid)
	{
	$mresult.=$sep.$ninfo[0].'|'.$ninfo[1].'|'.$ninfo[2];
	}
	
	if ($mresult!="")
	{
	$sep="_";
	}
	
	}
	}
	//Get binds ready to update the price
	$nbind['d']=$dresult;
	$nbind['w']=$wresult;
	$nbind['m']=$mresult;
	$nbind['id']=$aptid;
	//update the max discount array
	query("UPDATE `apartments` SET `co_daily`=?,`co_weekly`=?,`co_monthly`=? WHERE `id` = ?",$nbind);
	}
	else
	{
	return -1;
	}
	}
	
	//Delete Company Related period
	function codelperiod ($pid,$oid,$coid)
	{
	//bind for getting the apt. of a specific period
	$bindz['pid']=$pid;
	$bindz['cid']=$coid;
	$bindz['oid']=$oid;
	//get the list of apt. related to that period
	$ainfo=query("SELECT `apt` FROM `co_periods` WHERE `period_id` = ? AND co_id=? AND `owner_id`=? LIMIT 1",$bindz);	
	//Make sure that there's such a period
	if (!empty($ainfo))
	{
	$ainfo=$ainfo[0];
	//Check if the period has previous apt. 
	if ($ainfo['apt']!="")
	{
	$nainfo=explode("_",$ainfo['apt']);
	foreach ($nainfo as $aptid)
	{
	codelp($aptid,$pid);
	codelp($aptid,$pid);
	}
	}
	}
	else
	{
	return -1;
	}
	query("DELETE FROM `co_periods` WHERE `period_id` = ? AND `co_id`=? AND `owner_id`=?",$bindz);
	}
	
	function utf8ToUnicode($msg)
	{
	if ($msg!="")
	{
		$char["0a"]="000A";
		$char["0b"]="000B";
		$char["0c"]="000C";
		$char["0d"]="000D";
		$char["d880"]="0600";
		$char["d881"]="0601";
		$char["d882"]="0602";
		$char["d883"]="0603";
		$char["d884"]="0604";
		$char["d886"]="0606";
		$char["d887"]="0607";
		$char["d888"]="0608";
		$char["d889"]="0609";
		$char["d88a"]="060A";
		$char["d88b"]="060B";
		$char["d88c"]="060C";
		$char["d88d"]="060D";
		$char["d88e"]="060E";
		$char["d88f"]="060F";
		$char["d890"]="0610";
		$char["d891"]="0611";
		$char["d892"]="0612";
		$char["d893"]="0613";
		$char["d894"]="0614";
		$char["d895"]="0615";
		$char["d896"]="0616";
		$char["d897"]="0617";
		$char["d898"]="0618";
		$char["d899"]="0619";
		$char["d89a"]="061A";
		$char["d89b"]="061B";
		$char["d89c"]="061C";
		$char["d89e"]="061E";
		$char["d89f"]="061F";
		$char["d8a0"]="0620";
		$char["d8a1"]="0621";
		$char["d8a2"]="0622";
		$char["d8a3"]="0623";
		$char["d8a4"]="0624";
		$char["d8a5"]="0625";
		$char["d8a6"]="0626";
		$char["d8a7"]="0627";
		$char["d8a8"]="0628";
		$char["d8a9"]="0629";
		$char["d8aa"]="062A";
		$char["d8ab"]="062B";
		$char["d8ac"]="062C";
		$char["d8ad"]="062D";
		$char["d8ae"]="062E";
		$char["d8af"]="062F";
		$char["d8b0"]="0630";
		$char["d8b1"]="0631";
		$char["d8b2"]="0632";
		$char["d8b3"]="0633";
		$char["d8b4"]="0634";
		$char["d8b5"]="0635";
		$char["d8b6"]="0636";
		$char["d8b7"]="0637";
		$char["d8b8"]="0638";
		$char["d8b9"]="0639";
		$char["d8ba"]="063A";
		$char["d8bb"]="063B";
		$char["d8bc"]="063C";
		$char["d8bd"]="063D";
		$char["d8be"]="063E";
		$char["d8bf"]="063F";
		$char["d980"]="0640";
		$char["d981"]="0641";
		$char["d982"]="0642";
		$char["d983"]="0643";
		$char["d984"]="0644";
		$char["d985"]="0645";
		$char["d986"]="0646";
		$char["d987"]="0647";
		$char["d988"]="0648";
		$char["d989"]="0649";
		$char["d98a"]="064A";
		$char["d98b"]="064B";
		$char["d98c"]="064C";
		$char["d98d"]="064D";
		$char["d98e"]="064E";
		$char["d98f"]="064F";
		$char["d990"]="0650";
		$char["d991"]="0651";
		$char["d992"]="0652";
		$char["d993"]="0653";
		$char["d994"]="0654";
		$char["d995"]="0655";
		$char["d996"]="0656";
		$char["d997"]="0657";
		$char["d998"]="0658";
		$char["d999"]="0659";
		$char["d99a"]="065A";
		$char["d99b"]="065B";
		$char["d99c"]="065C";
		$char["d99d"]="065D";
		$char["d99e"]="065E";
		$char["d99f"]="065F";
		$char["d9a0"]="0660";
		$char["d9a1"]="0661";
		$char["d9a2"]="0662";
		$char["d9a3"]="0663";
		$char["d9a4"]="0664";
		$char["d9a5"]="0665";
		$char["d9a6"]="0666";
		$char["d9a7"]="0667";
		$char["d9a8"]="0668";
		$char["d9a9"]="0669";
		$char["d9aa"]="066A";
		$char["d9ab"]="066B";
		$char["d9ac"]="066C";
		$char["d9ad"]="066D";
		$char["d9ae"]="066E";
		$char["d9af"]="066F";
		$char["d9b0"]="0670";
		$char["d9b1"]="0671";
		$char["d9b2"]="0672";
		$char["d9b3"]="0673";
		$char["d9b4"]="0674";
		$char["d9b5"]="0675";
		$char["d9b6"]="0676";
		$char["d9b7"]="0677";
		$char["d9b8"]="0678";
		$char["d9b9"]="0679";
		$char["d9ba"]="067A";
		$char["d9bb"]="067B";
		$char["d9bc"]="067C";
		$char["d9bd"]="067D";
		$char["d9be"]="067E";
		$char["d9bf"]="067F";
		$char["da80"]="0680";
		$char["da81"]="0681";
		$char["da82"]="0682";
		$char["da83"]="0683";
		$char["da84"]="0684";
		$char["da85"]="0685";
		$char["da86"]="0686";
		$char["da87"]="0687";
		$char["da88"]="0688";
		$char["da89"]="0689";
		$char["da8a"]="068A";
		$char["da8b"]="068B";
		$char["da8c"]="068C";
		$char["da8d"]="068D";
		$char["da8e"]="068E";
		$char["da8f"]="068F";
		$char["da90"]="0690";
		$char["da91"]="0691";
		$char["da92"]="0692";
		$char["da93"]="0693";
		$char["da94"]="0694";
		$char["da95"]="0695";
		$char["da96"]="0696";
		$char["da97"]="0697";
		$char["da98"]="0698";
		$char["da99"]="0699";
		$char["da9a"]="069A";
		$char["da9b"]="069B";
		$char["da9c"]="069C";
		$char["da9d"]="069D";
		$char["da9e"]="069E";
		$char["da9f"]="069F";
		$char["daa0"]="06A0";
		$char["daa1"]="06A1";
		$char["daa2"]="06A2";
		$char["daa3"]="06A3";
		$char["daa4"]="06A4";
		$char["daa5"]="06A5";
		$char["daa6"]="06A6";
		$char["daa7"]="06A7";
		$char["daa8"]="06A8";
		$char["daa9"]="06A9";
		$char["daaa"]="06AA";
		$char["daab"]="06AB";
		$char["daac"]="06AC";
		$char["daad"]="06AD";
		$char["daae"]="06AE";
		$char["daaf"]="06AF";
		$char["dab0"]="06B0";
		$char["dab1"]="06B1";
		$char["dab2"]="06B2";
		$char["dab3"]="06B3";
		$char["dab4"]="06B4";
		$char["dab5"]="06B5";
		$char["dab6"]="06B6";
		$char["dab7"]="06B7";
		$char["dab8"]="06B8";
		$char["dab9"]="06B9";
		$char["daba"]="06BA";
		$char["dabb"]="06BB";
		$char["dabc"]="06BC";
		$char["dabd"]="06BD";
		$char["dabe"]="06BE";
		$char["dabf"]="06BF";
		$char["db80"]="06C0";
		$char["db81"]="06C1";
		$char["db82"]="06C2";
		$char["db83"]="06C3";
		$char["db84"]="06C4";
		$char["db85"]="06C5";
		$char["db86"]="06C6";
		$char["db87"]="06C7";
		$char["db88"]="06C8";
		$char["db89"]="06C9";
		$char["db8a"]="06CA";
		$char["db8b"]="06CB";
		$char["db8c"]="06CC";
		$char["db8d"]="06CD";
		$char["db8e"]="06CE";
		$char["db8f"]="06CF";
		$char["db90"]="06D0";
		$char["db91"]="06D1";
		$char["db92"]="06D2";
		$char["db93"]="06D3";
		$char["db94"]="06D4";
		$char["db95"]="06D5";
		$char["db96"]="06D6";
		$char["db97"]="06D7";
		$char["db98"]="06D8";
		$char["db99"]="06D9";
		$char["db9a"]="06DA";
		$char["db9b"]="06DB";
		$char["db9c"]="06DC";
		$char["db9d"]="06DD";
		$char["db9e"]="06DE";
		$char["db9f"]="06DF";
		$char["dba0"]="06E0";
		$char["dba1"]="06E1";
		$char["dba2"]="06E2";
		$char["dba3"]="06E3";
		$char["dba4"]="06E4";
		$char["dba5"]="06E5";
		$char["dba6"]="06E6";
		$char["dba7"]="06E7";
		$char["dba8"]="06E8";
		$char["dba9"]="06E9";
		$char["dbaa"]="06EA";
		$char["dbab"]="06EB";
		$char["dbac"]="06EC";
		$char["dbad"]="06ED";
		$char["dbae"]="06EE";
		$char["dbaf"]="06EF";
		$char["dbb0"]="06F0";
		$char["dbb1"]="06F1";
		$char["dbb2"]="06F2";
		$char["dbb3"]="06F3";
		$char["dbb4"]="06F4";
		$char["dbb5"]="06F5";
		$char["dbb6"]="06F6";
		$char["dbb7"]="06F7";
		$char["dbb8"]="06F8";
		$char["dbb9"]="06F9";
		$char["dbba"]="06FA";
		$char["dbbb"]="06FB";
		$char["dbbc"]="06FC";
		$char["dbbd"]="06FD";
		$char["dbbe"]="06FE";
		$char["dbbf"]="06FF";
		$char["20"]="0020";
		$char["21"]="0021";
		$char["22"]="0022";
		$char["23"]="0023";
		$char["24"]="0024";
		$char["25"]="0025";
		$char["26"]="0026";
		$char["27"]="0027";
		$char["28"]="0028";
		$char["29"]="0029";
		$char["2a"]="002A";
		$char["2b"]="002B";
		$char["2c"]="002C";
		$char["2d"]="002D";
		$char["2e"]="002E";
		$char["2f"]="002F";
		$char["30"]="0030";
		$char["31"]="0031";
		$char["32"]="0032";
		$char["33"]="0033";
		$char["34"]="0034";
		$char["35"]="0035";
		$char["36"]="0036";
		$char["37"]="0037";
		$char["38"]="0038";
		$char["39"]="0039";
		$char["3a"]="003A";
		$char["3b"]="003B";
		$char["3c"]="003C";
		$char["3d"]="003D";
		$char["3e"]="003E";
		$char["3f"]="003F";
		$char["40"]="0040";
		$char["41"]="0041";
		$char["42"]="0042";
		$char["43"]="0043";
		$char["44"]="0044";
		$char["45"]="0045";
		$char["46"]="0046";
		$char["47"]="0047";
		$char["48"]="0048";
		$char["49"]="0049";
		$char["4a"]="004A";
		$char["4b"]="004B";
		$char["4c"]="004C";
		$char["4d"]="004D";
		$char["4e"]="004E";
		$char["4f"]="004F";
		$char["50"]="0050";
		$char["51"]="0051";
		$char["52"]="0052";
		$char["53"]="0053";
		$char["54"]="0054";
		$char["55"]="0055";
		$char["56"]="0056";
		$char["57"]="0057";
		$char["58"]="0058";
		$char["59"]="0059";
		$char["5a"]="005A";
		$char["5b"]="005B";
		$char["5c"]="005C";
		$char["5d"]="005D";
		$char["5e"]="005E";
		$char["5f"]="005F";
		$char["60"]="0060";
		$char["61"]="0061";
		$char["62"]="0062";
		$char["63"]="0063";
		$char["64"]="0064";
		$char["65"]="0065";
		$char["66"]="0066";
		$char["67"]="0067";
		$char["68"]="0068";
		$char["69"]="0069";
		$char["6a"]="006A";
		$char["6b"]="006B";
		$char["6c"]="006C";
		$char["6d"]="006D";
		$char["6e"]="006E";
		$char["6f"]="006F";
		$char["70"]="0070";
		$char["71"]="0071";
		$char["72"]="0072";
		$char["73"]="0073";
		$char["74"]="0074";
		$char["75"]="0075";
		$char["76"]="0076";
		$char["77"]="0077";
		$char["78"]="0078";
		$char["79"]="0079";
		$char["7a"]="007A";
		$char["7b"]="007B";
		$char["7c"]="007C";
		$char["7d"]="007D";
		$char["7e"]="007E";
		$char["7f"]="007F";
		$char["c280"]="0080";
		$char["c281"]="0081";
		$char["c282"]="0082";
		$char["c283"]="0083";
		$char["c284"]="0084";
		$char["c285"]="0085";
		$char["c286"]="0086";
		$char["c287"]="0087";
		$char["c288"]="0088";
		$char["c289"]="0089";
		$char["c28a"]="008A";
		$char["c28b"]="008B";
		$char["c28c"]="008C";
		$char["c28d"]="008D";
		$char["c28e"]="008E";
		$char["c28f"]="008F";
		$char["c290"]="0090";
		$char["c291"]="0091";
		$char["c292"]="0092";
		$char["c293"]="0093";
		$char["c294"]="0094";
		$char["c295"]="0095";
		$char["c296"]="0096";
		$char["c297"]="0097";
		$char["c298"]="0098";
		$char["c299"]="0099";
		$char["c29a"]="009A";
		$char["c29b"]="009B";
		$char["c29c"]="009C";
		$char["c29d"]="009D";
		$char["c29e"]="009E";
		$char["c29f"]="009F";
		$char["c2a0"]="00A0";
		$char["c2a1"]="00A1";
		$char["c2a2"]="00A2";
		$char["c2a3"]="00A3";
		$char["c2a4"]="00A4";
		$char["c2a5"]="00A5";
		$char["c2a6"]="00A6";
		$char["c2a7"]="00A7";
		$char["c2a8"]="00A8";
		$char["c2a9"]="00A9";
		$char["c2aa"]="00AA";
		$char["c2ab"]="00AB";
		$char["c2ac"]="00AC";
		$char["c2ad"]="00AD";
		$char["c2ae"]="00AE";
		$char["c2af"]="00AF";
		$char["c2b0"]="00B0";
		$char["c2b1"]="00B1";
		$char["c2b2"]="00B2";
		$char["c2b3"]="00B3";
		$char["c2b4"]="00B4";
		$char["c2b5"]="00B5";
		$char["c2b6"]="00B6";
		$char["c2b7"]="00B7";
		$char["c2b8"]="00B8";
		$char["c2b9"]="00B9";
		$char["c2ba"]="00BA";
		$char["c2bb"]="00BB";
		$char["c2bc"]="00BC";
		$char["c2bd"]="00BD";
		$char["c2be"]="00BE";
		$chars=str_split($msg);
		$c="";
		$result="";
		foreach ($chars as $uchar)
		{
		//Get the char	
		$c.=bin2hex($uchar);
		//Make sure that it's correct
		if (isset($char["$c"]))
		{
		$result.=$char["$c"];
		$c="";
		}
		
		}
		return $result;
	}
	else
	{
	return false;
	}		
}

?>

